<?php

class MySQLDAL implements SimpleDALinterface {
	#if you want to plug in your own DAL, do so here
	#these functions just wrap the mysql_ functions.  This works great if you are using mysqlnd.

	private $conn=null;
	private $stmt=null;

	function __construct($server = null, $connect = true, $force_new = true) {
		$this->my_connect($server, $connect, $force_new);
	}

	function my_insert_id($conn = null) {
		if(isset($conn)) return my_insert_id($conn);
		if($this->conn) return my_insert_id($this->conn);
		return my_insert_id($this->conn);
	}

	function my_connect($server=null, $connect = true, $force_new = true) {
		if(!$server) return false;

		if(empty($server['port'])) {
			$port = 3306;
		} else {
			$port = $server['port'];
		}

		if(empty($server['user'])) $server['user'] = 'root';
		if(empty($server['password'])) $server['password'] = '';

		if(!$connect) return false;

		if(!empty($server['options'])) {
			$conn = mysql_connect($server['host'] . ':' . $port, $server['user'], $server['password'],$force_new,$server['options']);
		} else {
			$conn = mysql_connect($server['host'] . ':' . $port, $server['user'], $server['password'],$force_new);

		}

		#if(!empty($server['db'])) mysql_select_db($server['db']);
		$this->conn = $conn;
		return $conn;
	}

	function get_warnings($conn) {
		$sql = "show warnings";
		$result = false;
		$warnings = array();
		if(isset($conn)) $result=$this->my_query($sql, $conn);
		if($this->conn) $result=$this->my_query($sql, $this->conn);
		while($row = $this->my_fetch_assoc($result)) {
			$warnings[] = $row;
			
		}
		return $warnings;
	
	}

	function my_affected_rows($conn = null) {

		if(isset($conn)) {	
			return(mysql_affected_rows($conn));	
		} elseif($this->conn) {
			return(mysql_affected_rows($this->conn));	
		}
		return(mysql_affected_rows());	
		
	}

	function my_query($sql='', $conn=null) {
		if(isset($conn)) {	
			$this->stmt = mysql_query($sql, $conn);
		} elseif($this->conn) {
			$this->stmt = mysql_query($sql, $this->conn);
		} else {
			$this->stmt = mysql_query($sql);
		}
		return $this->stmt;
	}

	function my_unbuffered_query ( $sql, $conn = null ) {
		if(isset($conn)) {	
			$this->stmt = mysql_unbuffered_query($sql, $conn);
		} elseif($this->conn) {
			$this->stmt = mysql_unbuffered_query($sql, $this->conn);
		} else {
			$this->stmt = mysql_unbuffered_query($sql);
		}
		return $this->stmt;
	}

	function my_fetch_assoc($stmt=null) {
		if(!$stmt) $stmt=$this->stmt;
		return mysql_fetch_assoc($stmt);
	}

	function my_fetch_array($stmt=null) {
		if(!$stmt) $stmt=$this->stmt;
		return mysql_fetch_array($stmt);
	}

	function my_errno($conn = null) {
		if($conn) {
			return mysql_errno($conn);
		}
		if($this->conn) return mysql_errno($this->conn);

		return mysql_errno();
	}

	function my_error($conn = null) {
		if(isset($conn)) {
			return mysql_error($conn);
		}
		if($this->conn) return mysql_error($this->conn);

		return mysql_error();
	}

	function my_select_db($db, $conn=null) {
		if(isset($conn)) return mysql_select_db($db, $conn);
		if($this->conn) return mysql_select_db($db,$this->conn);

		return mysql_select_db($db);
	}

	function my_field_type ($result=null ,$field_offset=0) {
		if(!isset($result)) $result = $this->stmt;
		return mysql_field_type($result, $field_offset);
	}

	function my_free_result($stmt = null) {
		if($stmt) return mysql_free_result($stmt);
		if($this->stmt) return mysql_free_result($this->stmt);
		return true;
	}

	function my_close($conn = null) {
		if($conn) return mysql_close($conn);
		if($this->conn) return mysql_close($this->conn);
		return false;
	}

	function my_begin($conn = null) {
		if(!$conn) {
			return mysql_query('begin',$this->conn);
		} else {
			return mysql_query('begin',$conn);
		}
	}

	function my_autocommit($state = true, $conn = null) {
		if($state) $state = 'true'; else $state = 'false';
		if(!$conn) {
			return mysql_query("set autocommit=$state",$this->conn);
		} else {
			return mysql_query("set autocommit=$state",$conn);
		}
	}

	function my_commit($conn = null) {
		if($conn) {
			return mysql_query('commit', $conn);
		} else {
			return mysql_query('commit',$this->conn);
		}
	}

	function my_rollback($conn = null) {
		if($conn) {
			return mysql_query('rollback', $conn);
		} else {
			return mysql_query('rollback',$this->conn);
		}
	}

	function my_real_escape_string($string, $conn = null) {
		if($conn != null) {
			return mysql_real_escape_string($string, $conn);
		}
		if($this->conn) return mysql_real_escape_string($string,$this->conn);
		return mysql_real_escape_string($string);
	
	}

	function my_escape_string($string, $conn = null) {
		return $this->my_real_escape_string($string, $conn);
	}


	#END DAL
}
